import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import store from '../store'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 用户权限认证
const list = ['mine']
// const isLogin = false
// 全局前置守卫
router.beforeEach((to, from, next) => {
  const isLogin = store.state.user.isLogin
  console.log('是否登录：', isLogin)
  if (list.indexOf(to.name) !== -1 && !isLogin) {
    next({
      name: 'login',
      params: {
        to
      }
    })
  } else {
    next()
  }
})
export default router
